package ajjj.kiviq;

import javax.xml.bind.annotation.*;

/**
 * Represents a single result of a search
 * 
 * @author orjohan
 */
@XmlRootElement(name = "result")
public class SearchResult implements Comparable<SearchResult> {

    @XmlAttribute
    private String matchedWords;
    @XmlAttribute
    private String questionTitle;
    @XmlAttribute
    private String answer;
    
    //The relevance is set by the search engine
    //and used to compare and sort SearchResults
    private int relevance = 0;

    /**
     * Default constructor
     */
    public SearchResult() {
    }

    /**
     * Initialize the SearcResult with the necessary
     * properties
     * 
     * @param keywords The keywords that were matched
     * @param questionTitle The title of the Question found
     * @param answer The answer to the Question found
     */
    public SearchResult(String keywords, String questionTitle, String answer) {
        matchedWords = keywords;
        this.questionTitle = questionTitle;
        this.answer = answer;
    }

    /**
     * Simple toString() override
     * 
     * @return The question title and answer,
     * separated by a linefeed character
     */
    @Override
    public String toString() {
        return questionTitle + "\n" + answer;
    }

    /**
     * Get the answer to the question
     * @return The answer string
     */
    public String getAnswer() {
        return answer;
    }

    /**
     * Get the words that were matched
     * and formed this result
     * @return The matchedWords string
     */
    public String getMatchedWords() {
        return matchedWords;
    }

    /**
     * The title of the question
     * @return The questionTitle string
     */
    public String getQuestionTitle() {
        return questionTitle;
    }
    
    /**
     * Set the relevance of this SearchResult,
     * where a higher value means higher relevance.
     * SearchResults are sorted in ascending order, with
     * higher relevance positioned earlier
     * @param relevance The relevance of this result
     */
    public void setRelevance(int relevance) {
	this.relevance = relevance;
    }
    
    /**
     * Compares this SearchResult to another
     * @param o The other SearchResult
     * @return Less than 0 if this SearchResult
     * is more relevant, more than 0 if the other
     * SearchResult is more relevant and
     * 0 otherwise
     */
    @Override
    public int compareTo(SearchResult o) {
        return o.relevance - relevance;
    }
}
